package net.siren93.smh.action;

import net.siren93.smh.entity.EmployeeEntity;
import net.siren93.smh.entity.ProjectEntity;
import net.siren93.smh.entity.TaskEntity;
import net.siren93.smh.entity.request.TasksRequest;
import net.siren93.smh.entity.response.BaseResponse;
import net.siren93.smh.entity.response.TaskPageResponse;
import net.siren93.smh.entity.response.TasksResponse;
import net.siren93.smh.service.EmployeeService;
import net.siren93.smh.service.ProjectService;
import net.siren93.smh.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by siren93 on 15/10/14.
 */
@Controller
@RequestMapping("/task")
public class TaskAction {
    @Autowired
    TaskService taskService;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    ProjectService projectService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String projectList(){
        return "/myTasks";
    }

    @RequestMapping(value = "/tasksOfProject", method = RequestMethod.GET)
    public String tasksOfProject() {
        return "/tasks";
    }

    @RequestMapping(value = "/tasksOfEmployee", method = RequestMethod.POST)
    public @ResponseBody
    TaskPageResponse listTasksOfEmployee(@CookieValue(value = "id", required = true) int id,
                                     @RequestParam(value = "page", defaultValue = "1") int page,
                                     @RequestParam(value = "rows", defaultValue = "10") int rows) {
        List<TaskEntity> tasks = taskService.findByEmployee(id, page, rows);
        return getTaskPageResponse(tasks);
    }

    @RequestMapping(value = "/tasksOfProject", method = RequestMethod.POST)
    public @ResponseBody
    TaskPageResponse listTasksOfProject(@CookieValue(value = "projectId", required = true) int projectId,
                                           @RequestParam(value = "page", defaultValue = "1") int page,
                                           @RequestParam(value = "rows", defaultValue = "10") int rows){
        List<TaskEntity> tasks = taskService.findByProject(projectId, page, rows);
        return getTaskPageResponse(tasks);
    }

    private TaskPageResponse getTaskPageResponse(List<TaskEntity> tasks) {
        TaskPageResponse pageResponse = new TaskPageResponse();
        List<TasksResponse> list = new ArrayList<>();
        for(TaskEntity entity : tasks) {
            TasksResponse response = new TasksResponse();
            response.setId(entity.getId());
            response.setName(entity.getName());
            response.setCreateDate(entity.getCreateDate());
            response.setStatus(entity.getStatus());
            response.setPriority(entity.getPriority());
            response.setProjectId(entity.getProjectId());
            ProjectEntity project = projectService.findById(entity.getProjectId());
            response.setProjectName(project.getName());
            response.setEmployeeId(entity.getEmployeeId());
            EmployeeEntity employee = employeeService.findById(entity.getEmployeeId());
            response.setEmployeeName(employee.getName());
            list.add(response);
        }
        pageResponse.setTotal(taskService.listTask().size());
        pageResponse.setRows(list);
        return pageResponse;
    }

    @RequestMapping(value = "/delTasks", method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse deleteTasks(@CookieValue(value = "id", defaultValue = "0") int id, @RequestBody int[] tasks){
        if(id == 0) return new BaseResponse(BaseResponse.STATUS_FAILED, "您还没有登陆");
        try{
            for(int i : tasks) {
                taskService.delById(i);
            }
            return new BaseResponse(BaseResponse.STATUS_SUCCESS, "成功");
        } catch (Exception e) {
            return new BaseResponse(BaseResponse.STATUS_FAILED, e.getMessage());
        }
    }
    @RequestMapping(value = "addOrUpdateTasks", method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse addOrUpdateTasks(@CookieValue(value = "projectId", required = true)int projectId,
                                  @RequestBody TasksRequest[] tasks) {
        try{
            for(TasksRequest task : tasks) {
                TaskEntity entity = new TaskEntity();
                entity.setId(task.getId());
                entity.setName(task.getName());
                String dateStr = task.getCreateDate();
                SimpleDateFormat fm = new SimpleDateFormat("MM/dd/yy");
                java.util.Date date = fm.parse(dateStr);
                entity.setCreateDate(new Date(date.getTime()));
                entity.setStatus(task.getStatus());
                entity.setPriority(task.getPriority());
                entity.setProjectId(projectId);
                entity.setEmployeeId(task.getEmployeeId());
                taskService.saveOrUpdate(entity);
            }
            return new BaseResponse(BaseResponse.STATUS_SUCCESS, "成功");
        }catch (Exception e) {
            return new BaseResponse(BaseResponse.STATUS_FAILED, e.getMessage());
        }
    }
}
